package dict_service

import (
	"errors"
	"starry_sky/internal/dal"
	"starry_sky/internal/model"
	"starry_sky/internal/tools/logger"
)

func DeleteDictType(dictType *model.DictType) error {
	err := dal.FindById(dictType)
	if logger.HasError(err) {
		return errors.New("查询字典类型失败")
	}
	var count int64
	err = dal.CountByMap[model.DictData](&count, map[string]interface{}{"dict_type": dictType.Type})
	if logger.HasError(err) {
		return errors.New("查询字典数据失败")
	}
	if count > 0 {
		return errors.New("字典类型下存在字典数据")
	}
	err = dal.Delete(&dictType)
	if logger.HasError(err) {
		return errors.New("删除字典类型失败")
	}
	return nil
}
